//
//  PlayerManager.m
//  OggSpeex
//
//  Created by Jiang Chuncheng on 6/25/13.
//  Copyright (c) 2013 Sense Force. All rights reserved.
//

#import "PlayerManager.h"
#import "amrFileCodec.h"
#import <UIKit/UIKit.h>

@interface PlayerManager ()



@end

@implementation PlayerManager
{
    NSString* _playingFileName;
}
@synthesize decapsulator;
@synthesize avAudioPlayer;

static PlayerManager *mPlayerManager = nil;

+ (PlayerManager *)sharedManager {
    static PlayerManager *g_playerManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        g_playerManager = [[PlayerManager alloc] init];
    });
    return g_playerManager;
}


- (instancetype)init
{
    if (self = [super init]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:mPlayerManager selector:@selector(sensorStateChange:) name:@"UIDeviceProximityStateDidChangeNotification"
                                                   object:nil];
        
        //初始化播放器的时候如下设置
        UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
        AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
        AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
        
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        //默认情况下扬声器播放
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
        [audioSession setActive:YES error:nil];
    }
    return self;
}

- (void)playAudioWithFileName:(NSString *)filename delegate:(id<PlayingDelegate>)newDelegate iOS:(BOOL)iOS {
    if ( ! filename) {
        return;
    }
    
    NSLog(@"播放的音频路径: %@", filename);
//    NSString *filePath = [NSString stringWithFormat:@"%@.wav", filename];
//    if ([[NSFileManager defaultManager] fileExistsAtPath:filename]) {
//        [NSFileManager.defaultManager copyItemAtPath:filename toPath:filePath error:nil];
//    }
//    filename = filePath;
    
    NSError *error;
    NSData *amrData = [NSData dataWithContentsOfFile:filename];
    NSData *wavData = DecodeAMRToWAVE(amrData);
    
    if (!iOS) {
        // 来自安卓
        NSError *error;
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filename] error:&error];
        if (self.avAudioPlayer) {
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            self.avAudioPlayer.delegate = self;
            _playingFileName = [filename copy];
            [self.avAudioPlayer play];
            [self startProximityMonitering];
        }
        else {
            [self.delegate playingStoped];
        }
        
        return;
    }
    
    // 来自iOS
    self.avAudioPlayer = [[AVAudioPlayer alloc] initWithData:wavData error:&error];
    if (self.avAudioPlayer) {
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[AVAudioSession sharedInstance] setActive:YES error:nil];
        self.avAudioPlayer.delegate = self;
        _playingFileName = [filename copy];
        [self.avAudioPlayer play];
        [self startProximityMonitering];
    }
    else {
        [self.delegate playingStoped];
    }
    
    
    return;
    if ([filename rangeOfString:@"http"].location != NSNotFound ||
        [filename rangeOfString:@"https"].location != NSNotFound ) { // 网络音频
        
        NSURL * url  = [NSURL URLWithString:filename];
        //说明网络文件是.amr的文件，需要转换格式
        if ([filename rangeOfString:@".amr"].location != NSNotFound){
            [self.delegate playingStoped];
            self.delegate = newDelegate;
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSError *error;
                NSData *amrData = [NSData dataWithContentsOfURL:url];
                NSData *wavData = DecodeAMRToWAVE(amrData);
                self.avAudioPlayer = [[AVAudioPlayer alloc] initWithData:wavData error:&error];
                if (self.avAudioPlayer) {
                    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
                    [[AVAudioSession sharedInstance] setActive:YES error:nil];
                    self.avAudioPlayer.delegate = self;
                    _playingFileName = [filename copy];
                    [self.avAudioPlayer play];
                    [self startProximityMonitering];
                }
                else {
                    [self.delegate playingStoped];
                }
            });

            
        }else{
            
            AVPlayerItem *songItem = [[AVPlayerItem alloc] initWithURL:url];
            AVPlayer * player = [[AVPlayer alloc]initWithPlayerItem:songItem];
            
            [player play];
        }
        
    }
    
    else if ([filename rangeOfString:@".spx"].location != NSNotFound) {
        
        [[AVAudioSession sharedInstance] setActive:YES error:nil];
        
        [self stopPlaying];
        
        self.delegate = newDelegate;
        
        self.decapsulator = [[Decapsulator alloc] initWithFileName:filename];
        self.decapsulator.delegate = self;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [self startProximityMonitering];
        _playingFileName = [filename copy];
        [self.decapsulator play];
        
    } else if ([filename rangeOfString:@".amr"].location != NSNotFound)
    {
        if (![[NSFileManager defaultManager] fileExistsAtPath:filename])
        {
            NSLog(@"要播放的文件不存在:%@", filename);
            _playingFileName = nil;
            [self.delegate playingStoped];
            [newDelegate playingStoped];
            return;
        }
        [self.delegate playingStoped];
        self.delegate = newDelegate;
        
        NSError *error;
        NSData *amrData = [NSData dataWithContentsOfFile:filename];
        NSData *wavData = DecodeAMRToWAVE(amrData);
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithData:wavData error:&error];
        if (self.avAudioPlayer) {
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            self.avAudioPlayer.delegate = self;
            _playingFileName = [filename copy];
            [self.avAudioPlayer play];
            [self startProximityMonitering];
        }
        else {
            [self.delegate playingStoped];
        }
    }
    else if ([filename rangeOfString:@".mp3"].location != NSNotFound ||
             [filename rangeOfString:@".wav"].location != NSNotFound )
    {
        if (![[NSFileManager defaultManager] fileExistsAtPath:filename])
        {
            NSLog(@"要播放的文件不存在:%@", filename);
            _playingFileName = nil;
            [self.delegate playingStoped];
            [newDelegate playingStoped];
            return;
        }
        [self.delegate playingStoped];
        self.delegate = newDelegate;
        
        NSError *error;
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filename] error:&error];
        if (self.avAudioPlayer) {
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            self.avAudioPlayer.delegate = self;
            _playingFileName = [filename copy];
            [self.avAudioPlayer play];
            [self startProximityMonitering];
        }
        else {
            [self.delegate playingStoped];
        }
    }
    else {
        [self.delegate playingStoped];
    }
}

- (void)playAudioWithFileName:(NSString *)filename playerType:(PlayerType)type delegate:(id<PlayingDelegate>)newDelegate
{
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    if ([filename rangeOfString:@".spx"].location != NSNotFound) {
        [self stopPlaying];
        self.delegate = newDelegate;
        
        self.decapsulator = [[Decapsulator alloc] initWithFileName:filename];
        self.decapsulator.delegate = self;
        [self.decapsulator play];
        
    }else if ([filename rangeOfString:@".amr"].location != NSNotFound)
    {
        if (![[NSFileManager defaultManager] fileExistsAtPath:filename])
        {
            NSLog(@"要播放的文件不存在:%@", filename);
            _playingFileName = nil;
            [self.delegate playingStoped];
            [newDelegate playingStoped];
            return;
        }
        [self.delegate playingStoped];
        self.delegate = newDelegate;
        
        NSError *error;
        NSData *amrData = [NSData dataWithContentsOfFile:filename];
        NSData *wavData = DecodeAMRToWAVE(amrData);
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithData:wavData error:&error];
        if (self.avAudioPlayer) {
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            self.avAudioPlayer.delegate = self;
            _playingFileName = [filename copy];
            [self.avAudioPlayer play];
            [self startProximityMonitering];
        }
        else {
            [self.delegate playingStoped];
        }
    }
    else if ([filename rangeOfString:@".mp3"].location != NSNotFound ||
             [filename rangeOfString:@".wav"].location != NSNotFound)
    {
        if (![[NSFileManager defaultManager] fileExistsAtPath:filename]) {
            NSLog(@"playerType :%ld 要播放的文件不存在:%@", (unsigned long)type,filename);
            _playingFileName = nil;
            [self.delegate playingStoped];
            [newDelegate playingStoped];
            return;
        }
        _playingFileName = nil;
        [self.delegate playingStoped];
        self.delegate = newDelegate;
        
        NSError *error;
        
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filename] error:&error];
        if (self.avAudioPlayer) {
            self.avAudioPlayer.delegate = self;
            [self.avAudioPlayer play];
        }
        else {
            _playingFileName = nil;
            [self.delegate playingStoped];
        }
    }else if([filename rangeOfString:@".caf"].location != NSNotFound)
    {
        _playingFileName = nil;
        [self.delegate playingStoped];
        self.delegate = newDelegate;
        
        NSError *error;
        NSArray *array = [filename componentsSeparatedByString:@"."];
        NSString *bundlePath = [[NSBundle mainBundle]pathForResource:@"Resource" ofType:@"bundle"];
        NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
        NSString *soundPath = [bundle pathForResource:array[0] ofType:@"caf"inDirectory:nil];
        if (soundPath == nil) {
            NSLog(@"要播放的文件不存在:%@", filename);
            [self.delegate playingStoped];
            [newDelegate playingStoped];
            return;
        }
        NSURL *soundUrl = [[NSURL alloc] initFileURLWithPath:soundPath];
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
        if (self.avAudioPlayer) {
            self.avAudioPlayer.delegate = self;
            [self.avAudioPlayer play];
        }
        else {
            _playingFileName = nil;
            [self.delegate playingStoped];
        }
    }
    else {
        _playingFileName = nil;
        [self.delegate playingStoped];
    }
    
    switch (type)
    {
        case DDEarPhone:
            //听筒
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
            break;
        case DDSpeaker:
            //扬声器
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            break;
        default:
            break;
    }
}

- (void)stopPlaying
{
    _playingFileName = nil;
    [self stopProximityMonitering];

    if (self.decapsulator) {
        [self.decapsulator stopPlaying];
        self.decapsulator = nil;//此行如果放在上一行之前会导致回调问题
    }
    if (self.avAudioPlayer) {
        [self.avAudioPlayer stop];
        self.avAudioPlayer = nil;
    }
}

- (BOOL)playingFileName:(NSString *)fileName
{
    return [_playingFileName isEqualToString:fileName];
}

- (BOOL)playing
{
    return _playingFileName.length;
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    _playingFileName = nil;
    [self.delegate playingStoped];
    [self stopProximityMonitering];
}


- (void)decapsulatingAndPlayingOver {
    _playingFileName = nil;
    [self.delegate playingStoped];
    [self stopProximityMonitering];
}

- (void)sensorStateChange:(NSNotification *)notification { 
    //如果此时手机靠近面部放在耳朵旁，那么声音将通过听筒输出，并将屏幕变暗
    if ([[UIDevice currentDevice] proximityState] == YES) {
        NSLog(@"Device is close to user");
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    }
    else {
        NSLog(@"Device is not close to user");
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    }
}

/**
 *  开启距离感应器监听(开始播放时)
 */
- (void)startProximityMonitering {
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
    NSLog(@"开启距离监听");
}

/**
 *  关闭距离感应器监听(播放完成时)
 */
- (void)stopProximityMonitering {

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
        NSLog(@"关闭距离监听");
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
